package jzoffer;

import java.util.Arrays;

/**
 * @author MaoLin Wang
 * @date 2020/10/8 8:49 上午
 */
public class _21调整数组顺序使奇数位于偶数前面 {
    public static int[] exchange(int[] nums) {
        if (nums==null || nums.length<=1){
            return nums;
        }
        int left=-1;
        int l=0;
        int right=nums.length;
        int r=right-1;

        while (true){
            while ((nums[++left]&1)==1){
                if (left>=r){
                    break;
                }
            }
            while ((nums[--right]&1)==0){
                if(right<=l){
                    break;
                }
            }
            if (left>=right){
                break;
            }
            int temp=nums[left];
            nums[left]=nums[right];
            nums[right]=temp;
        }
        return nums;
    }

    public static void main(String[] args) {
        int[]nums=new int[]{1,3,5};
        int[] exchange = exchange(nums);
        Arrays.stream(exchange).forEach(System.out::println);
    }
}
